package cn.xspace.module.other.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cn.wt.wtutils.utils.ToastUtils;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.network.exception.HttpException;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.ResourceUtils;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpUtils;
import cn.xspace.module.other.R;
import cn.xspace.module.other.util.RProxy;
import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SignatureAct extends BaseActivity {

    private String userId;
    private TitleBar mTitleBar;
    private EditText mEtMyname;
    private TextView mSignatureWordNumb;
    private String myname;
    private int num = 20;
    private int number;


    @Override
    protected void getIntentData() {
        myname = getIntent().getStringExtra("SignatureAct");
    }

    @Override
    protected void initView() {
        setContentView(R.layout.signatureact);

    }

    @Override
    protected void initFindViewById() {
        mTitleBar = (TitleBar) findViewById(R.id.signature_title_bar);
        mEtMyname = (EditText) findViewById(RProxy.et_myname);
        mSignatureWordNumb = (TextView) findViewById(R.id.signature_word_numb);
        if (!ResourceUtils.getString(R.string.userinfoact_not_filled).equals(myname)) {
            mEtMyname.setText(myname);
        }
        mEtMyname.addTextChangedListener(new NotEmptyTextWatcher());
    }

    @Override
    protected void initData() {
        userId = MyInfoUtil.getMyUserid(this);
    }

    @Override
    protected void initEvent() {
        String contant = mEtMyname.getText().toString().trim();
        mSignatureWordNumb.setText(ResourceUtils.getString(R.string.surplus) + (num - contant.length()) +
                ResourceUtils.getString(R.string.word));
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String signature = mEtMyname.getText().toString().trim();
                if (TextUtils.isEmpty(signature)) {
                    signature = ResourceUtils.getString(R.string.userinfoact_not_filled);
                }
                Map<String, String> map = new HashMap<>();
                map.put("type", "SignatureAct");
                map.put("SignatureAct", signature);
                EventBus.getDefault().post(map);
                requestData(signature);
                finish();
            }
        });
    }

    private void requestData(String signature) {
        final Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("user_desc", signature);
        EventBus.getDefault().post(map);
        Observable.create(new Observable.OnSubscribe<Object>() {
            @Override
            public void call(Subscriber<? super Object> subscriber) {
                BaseReturnBean bean = OkHttpUtils.initHttp(Constants.UPDATE_USER_SIGN, map);
                if (OkHttpUtils.isSuccess(bean)) {
                    subscriber.onNext(bean);
                } else {
                    subscriber.onError(new HttpException());
                }
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
            }

            @Override
            public void onNext(Object result) {
                if (result != null) {
//                    T.showSingleL("签名修改成功");
                }
            }
        });
    }


    /**
     * Text watcher to test if all fields are field.
     */
    private CharSequence temp;

    private class NotEmptyTextWatcher implements TextWatcher {

        @Override
        public void afterTextChanged(Editable s) {
            number = num - s.length();
            mSignatureWordNumb.setText("剩余" + number + "字");
            int selectionStart = mEtMyname.getSelectionStart();
            int selectionEnd = mEtMyname.getSelectionEnd();
            if (temp.length() > num) {
                ToastUtils.show(SignatureAct.this, "字数已达到最大限制");
                s.delete(selectionStart - 1, selectionEnd);
                mEtMyname.setText(s);
                mEtMyname.setSelection(selectionStart);//设置光标在最后
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            temp = s;
        }

    }

}
